package com.qiantu.phone.ui.dialog;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bigkoo.pickerview.adapter.ArrayWheelAdapter;
import com.contrarywind.view.WheelView;
import com.qiantu.api.db.LLControllerDBManager;
import com.qiantu.api.entity.ControllerBean;
import com.qiantu.api.entity.DeviceBean;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppApplication;
import com.qiantu.phone.bean.TimeLimitBean;
import com.lxj.xpopup.core.BottomPopupView;

import java.util.ArrayList;
import java.util.List;

public class BottomTimeLimitDialog extends BottomPopupView {
    public final static int TYPE_HOUR = 0x1;
    public final static int TYPE_DIMMING_CYCLE_SECOND = 0x2;
    //dali调光周期
    private float[] tctdlDimmingCycleSeconds;
    public ControllerBean mController;

    private DialogClickListener mDialogClickListener;
    private TextView tvLabel;
    private String label;
    private WheelView wv_first;
    private ArrayWheelAdapter mFirstArrayWheelAdapter;
    private int type = TYPE_HOUR;
    private float currentItemValue;
    private int defaultIndex;

    public BottomTimeLimitDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.dialog_time_limit;
    }

    // 执行初始化操作，比如：findView，设置点击，或者任何你弹窗内的业务逻辑
    @Override
    protected void onCreate() {
        super.onCreate();
        tvLabel = findViewById(R.id.tv_label);
        wv_first = findViewById(R.id.options1);
        View btnSave = findViewById(R.id.btn_save);
        btnSave.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mDialogClickListener != null) {
                    TimeLimitBean timeLimitBean = (TimeLimitBean) mFirstArrayWheelAdapter.getItem(wv_first.getCurrentItem());
                    mDialogClickListener.onConfirmClick(timeLimitBean);
                }
            }
        });
        tvLabel.setText(label);
        setStyle();
        initWheelView();
    }

    public void setLabel(String label) {
        this.label = label;
        if (tvLabel != null) {
            tvLabel.setText(label);
        }
    }

    public void setType(int type) {
        this.type = type;
    }

    public void setDevice(DeviceBean mDevice){
        mController = LLControllerDBManager.getInstance(getContext()).getControllerBySerialNo(mDevice.getControllerSerialNo());
        if(mController.getProductModel().equals("0x0002")){
            tctdlDimmingCycleSeconds = new float[]{0, 0.7f, 1, 1.4f, 2, 2.8f, 4, 5.7f, 8, 11.3f, 16, 22.6f, 32, 45.3f, 64, 90.5f};
            defaultIndex = 6;
        }else{
            tctdlDimmingCycleSeconds = new float[]{0,  1, 2, 3, 4, 5, 6, 7, 8, 10, 20, 30, 60};
            defaultIndex = 4;
        }
    }

    private void setStyle() {
        wv_first.setGravity(Gravity.CENTER);
        wv_first.setCyclic(false);
        wv_first.setAlphaGradient(true);
//        wv_first.setLabelOffset(5);
        wv_first.setItemsVisibleCount(5);
        wv_first.setDividerColor(0x00000000);
        wv_first.setTextColorCenter(AppApplication.getInstance().getThemeColor(R.attr.themeColor));
        wv_first.setTextSize(getResources().getDimensionPixelSize(R.dimen.dp_18));
    }

    private void initWheelView() {
        List<TimeLimitBean> items = new ArrayList<>();
        int currentItem = 0;
        if (type == TYPE_HOUR) {
            TimeLimitBean timeLimitBean1 = new TimeLimitBean();
            timeLimitBean1.setLabel("3小时");
            int value1 = 3 * 60 * 60 * 1000;
            if(currentItemValue == value1){
                currentItem = 0;
            }
            timeLimitBean1.setValue(value1 + "");
            items.add(timeLimitBean1);
            TimeLimitBean timeLimitBean2 = new TimeLimitBean();
            timeLimitBean2.setLabel("6小时");
            int value2 = 6 * 60 * 60 * 1000;
            if(currentItemValue == value2){
                currentItem = 1;
            }
            timeLimitBean2.setValue(value2 + "");
            items.add(timeLimitBean2);
            TimeLimitBean timeLimitBean3 = new TimeLimitBean();
            timeLimitBean3.setLabel("12小时");
            int value3 = 12 * 60 * 60 * 1000;
            if(currentItemValue == value3){
                currentItem = 2;
            }
            timeLimitBean3.setValue(value3 + "");
            items.add(timeLimitBean3);
            TimeLimitBean timeLimitBean4 = new TimeLimitBean();
            timeLimitBean4.setLabel("24小时");
            int value4 = 24 * 60 * 60 * 1000;
            if(currentItemValue == value4){
                currentItem = 3;
            }
            timeLimitBean4.setValue(value4+ "");
            items.add(timeLimitBean4);
            TimeLimitBean timeLimitBean5 = new TimeLimitBean();
            timeLimitBean5.setLabel("2天");
            int value5 = 48 * 60 * 60 * 1000;
            if(currentItemValue == value5){
                currentItem = 4;
            }
            timeLimitBean5.setValue(value5 + "");
            items.add(timeLimitBean5);
        } else if (type == TYPE_DIMMING_CYCLE_SECOND) {
            currentItem = defaultIndex;
            for(int i=0; i<tctdlDimmingCycleSeconds.length; i++){
                TimeLimitBean timeLimitBean = new TimeLimitBean();
                String dimmingCycleStr = "";
                if (tctdlDimmingCycleSeconds[i] * 10 % 10 == 0) {
                    dimmingCycleStr = (int) tctdlDimmingCycleSeconds[i] + "";
                } else {
                    dimmingCycleStr = tctdlDimmingCycleSeconds[i] + "";
                }
                if(tctdlDimmingCycleSeconds[i] == 0){
                    timeLimitBean.setLabel(getResources().getString(R.string.immediately));
                }else if(i == defaultIndex){
                    timeLimitBean.setLabel(dimmingCycleStr + "s默认");
                }else{
                    timeLimitBean.setLabel(dimmingCycleStr + "s");
                }
                timeLimitBean.setNumberValue(tctdlDimmingCycleSeconds[i]);
                items.add(timeLimitBean);
                if(tctdlDimmingCycleSeconds[i] == currentItemValue){
                    currentItem = i;
                }
            }
        }
        mFirstArrayWheelAdapter = new ArrayWheelAdapter(items);
        wv_first.setAdapter(mFirstArrayWheelAdapter);
        wv_first.setCurrentItem(currentItem);
    }

    public void setSelectedItem(float currentItemValue){
        this.currentItemValue = currentItemValue;
        if(currentItemValue == -1){
            if(wv_first != null){
                this.currentItemValue = tctdlDimmingCycleSeconds[defaultIndex];
                wv_first.setCurrentItem(defaultIndex);
            }
        }else{
            if(wv_first != null){
                for(int i=0; i<mFirstArrayWheelAdapter.getItemsCount(); i++){
                    TimeLimitBean timeLimitBean = (TimeLimitBean) mFirstArrayWheelAdapter.getItem(i);
                    if(timeLimitBean.getNumberValue() == currentItemValue){
                        wv_first.setCurrentItem(i);
                    }
                }
            }
        }
    }

    // 设置最大宽度，看需要而定
    @Override
    protected int getMaxWidth() {
        return super.getMaxWidth();
    }

    // 设置最大高度，看需要而定
    @Override
    protected int getMaxHeight() {
        return super.getMaxHeight();
    }

    /**
     * 弹窗的宽度，用来动态设定当前弹窗的宽度，受getMaxWidth()限制
     *
     * @return
     */
    protected int getPopupWidth() {
        return 0;
    }

    /**
     * 弹窗的高度，用来动态设定当前弹窗的高度，受getMaxHeight()限制
     *
     * @return
     */
    protected int getPopupHeight() {
        return 0;
    }

    public void setDialogClickListener(@Nullable DialogClickListener listener) {
        mDialogClickListener = listener;
    }

    public interface DialogClickListener {
        void onConfirmClick(TimeLimitBean timeLimitBean);
    }
}
